﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using EventHandler.Libraries.Business.Database;
using Rhino.Mocks;
using EventHandler.Libraries.Business.Event;
using System.Data;

namespace EventHandler.Libraries.Business.Tests.Event
{
    [TestFixture]
    class RegisterForEventTest
    {
        private static IDatabaseHelper CreateDatabaseHelper()
        {
            return MockRepository.GenerateMock<IDatabaseHelper>();
        }

        private static IRegisterEventItem GetValidRegisterForEventItem()
        {
            IRegisterEventItem registerForEventItem = new RegisterForEventItem
            {
                EventId = 1,
                OwnerId = 1
            };
            return registerForEventItem;
        }

        private static IRegisterEventItem GetInValidRegisterForEventItem()
        {
            IRegisterEventItem registerForEventItem = new RegisterForEventItem
            {
                EventId = -1,
                OwnerId = -1
            };
            return registerForEventItem;
        }

        private static void RegisterForEventItem(IRegisterEventItem testEventItem)
        {
            IDatabaseHelper dbHelper = CreateDatabaseHelper();
            var helper = new EventItemHelper(dbHelper);

            var cn = MockRepository.GenerateMock<IDbConnection>();
            dbHelper.Expect(x => x.GetConnection()).Return(cn);

            var cmd = MockRepository.GenerateMock<IDbCommand>();
            cn.Expect(x => x.CreateCommand()).Return(cmd);
            cmd.Expect(x => x.CommandText).PropertyBehavior();
            cmd.Expect(x => x.CommandType).PropertyBehavior();

            var parameters = MockRepository.GenerateMock<IDataParameterCollection>();
            cmd.Expect(x => x.Parameters).Return(parameters).Repeat.Any();

            var eventIdParameter = MockRepository.GenerateMock<IDbDataParameter>();
            cmd.Expect(x => x.CreateParameter()).Return(eventIdParameter);
            eventIdParameter.Expect(x => x.ParameterName).PropertyBehavior();
            eventIdParameter.Expect(x => x.DbType).PropertyBehavior();
            eventIdParameter.Expect(x => x.Value).Return(1);
            var ownerIdParameter = MockRepository.GenerateMock<IDbDataParameter>();
            cmd.Expect(x => x.CreateParameter()).Return(ownerIdParameter);
            ownerIdParameter.Expect(x => x.ParameterName).PropertyBehavior();
            ownerIdParameter.Expect(x => x.DbType).PropertyBehavior();
            ownerIdParameter.Expect(x => x.Value).Return(1);


            helper.RegisterForEvent(testEventItem);

            cn.AssertWasCalled(x => x.Open());
            cn.AssertWasCalled(x => x.Close());

            Assert.AreEqual("Auth.RegisterForEvent", cmd.CommandText);
            Assert.AreEqual(CommandType.StoredProcedure, cmd.CommandType);

            Assert.AreEqual("@OwnerID", ownerIdParameter.ParameterName);
            Assert.AreEqual(testEventItem.OwnerId, ownerIdParameter.Value);
            Assert.AreEqual(DbType.Int32, ownerIdParameter.DbType);
            Assert.AreEqual("@EventID", eventIdParameter.ParameterName);
            Assert.AreEqual(DbType.Int32, eventIdParameter.DbType);
            Assert.AreEqual(testEventItem.EventId, eventIdParameter.Value);
        }

        [Test]
        public void TestEventDeletion()
        {
            RegisterForEventItem(GetValidRegisterForEventItem());
        }
    }
}
